package com.accounting.client.frontend.customer.dialog;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.Sex;
import com.accounting.client.remote.obj.Customer;
import com.accounting.client.remote.transfer.UpdateCustomerRequest;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.delegate.Action;
import corelib.client.resource.CommonResources;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.LazyInitializer;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.TextAreaField;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.layout.RelativeColumn;

public class UpdateCustomerHandler extends SimpleDialogHandler {
	
	private TextField nameField;
	private TextField cNameField;
	private SelectField<Integer> sexField;
	
	private TextField emailField1;
	private TextAreaField  addressField1;
	private TextField phoneNumberField1;
	
	private TextField emailField2;
	private TextField phoneNumberField2;
	private TextAreaField  addressField2;
	
	private Customer customerInfo;
	private Action action;
	
	public UpdateCustomerHandler(Customer info, Action action) {
		this.customerInfo = info;
		this.action = action;
	}
	
	@Override
	public String getHeading() {
		return "更新客户信息";
	}

	@Override
	public Size getSize() {
		return new Size(800, 400);
	}

	@Override
	public Widget getContentPanel() {
		int lw = 120;
		int fw = 250;
		int gap = 8;
		FlowPanel wrapper = new FlowPanel();
		wrapper.getElement().getStyle().setPadding(15, Unit.PX);
		
		RelativeColumn col = new RelativeColumn();
		col.setRowSpacing(12);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("姓名"), lw)
		   .addSpace(gap)
		   .add(nameField  = LazyInitializer.createTextBox("英文"),fw)
		   .addSpace(gap)
		   .add(cNameField = LazyInitializer.createTextBox("中文"),fw);
		
		col.setRowSpacing(5);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("性别"),lw)
		   .addSpace(gap)
		   .add(sexField = LazyInitializer.createSelectBox(Sex.All, new ValueFormatter<Integer>(){
				@Override
				public String format(Integer value) {
					return Sex.getName(value);
				}
		}));
		
		col.addRow()
		   .add(LazyInitializer.createLabel("电子邮件地址"),lw)
		   .addSpace(gap)
		   .add(emailField1 = LazyInitializer.createTextBox("电子邮件地址 1"),fw)
		   .addSpace(gap)
		   .add(emailField2 = LazyInitializer.createTextBox("电子邮件地址 2"), fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("电话号码"),lw)
		   .addSpace(gap)
		   .add(phoneNumberField1 = LazyInitializer.createTextBox("电话号码 1"),fw)
		   .addSpace(gap)
		   .add(phoneNumberField2 = LazyInitializer.createTextBox("电话号码 2"),fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("地址"), lw)
		   .addSpace(gap)
		   .add(addressField1 = LazyInitializer.createTextArea(), fw)
		   .addSpace(gap)
		   .add(addressField2 = LazyInitializer.createTextArea(), fw);
		
		addressField1.setHeight(150);
		addressField2.setHeight(150);
		
		sync();
		wrapper.add(col.getPanel());
		return wrapper;
	}
	
	private void sync() {
		nameField.setText(customerInfo.getName(), false);
		cNameField.setText(customerInfo.getCName(),false);
		sexField.setSelectedOption(customerInfo.getSex(),false);
		emailField1.setText(customerInfo.getEmail1(),false);
		phoneNumberField1.setText(customerInfo.getPhoneNumber1(), false);
		addressField1.setValue(customerInfo.getAddress1());
		emailField2.setText(Util.getString(customerInfo.getEmail2()), false);
		phoneNumberField2.setText(Util.getString(customerInfo.getPhoneNumber2()), false);
		addressField2.setValue(Util.getString(customerInfo.getAddress2()));
	}
	
	@Override
	public IsButton[] getButtons() {
		return new IsButton[]{
			new IsButton("OK",CommonResources.INSTANCE.accept16(),
				new WidgetListener(){
					@Override
					public void valueUpdated(Widget widget) {
						update();
					}
				}
			)	
		};
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	private void update() {
		UpdateCustomerRequest request = new UpdateCustomerRequest();
		request.setCustomerId(customerInfo.getId());
		request.setAddress1(addressField1.getValue());
		request.setCName(cNameField.getTrimmedText());
		request.setName(nameField.getTrimmedText());
		request.setEmail1(emailField1.getTrimmedText());
		request.setPhoneNumber1(phoneNumberField1.getTrimmedText());
		request.setSex(sexField.getSelectedOption());
		request.setEmail2(emailField2.getTrimmedText());
		request.setPhoneNumber2(phoneNumberField2.getTrimmedText());
		request.setAddress2(addressField2.getValue());
		
		if (Util.isBlank(addressField1.getValue())){
			addressField1.markRequired();
			return;
		}
		
		if (Util.isBlank(cNameField.getTrimmedText())){
			cNameField.markRequired();
			return;
		}
		
		if (Util.isBlank(nameField.getTrimmedText())){
			nameField.markRequired();
			return;
		}
		
		if (Util.isBlank(phoneNumberField1.getTrimmedText())){
			phoneNumberField1.markRequired();
			return;
		}
		
		String passport = SessionManager.getPassport();
		Services.CustomerService.UpdateCustomer(passport, request,
			new AsyncCallback<Boolean>(){
				@Override
				public void onFailure(Throwable caught) {
					ErrorHandler.handleServiceError(caught);
				}
				@Override
				public void onSuccess(Boolean result) {
					if (result == Boolean.TRUE){
						closeDialog();
						if (action != null){
							action.run();
						}
					}
				}
			}
		);
	}
}
